package com.lele.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lele.pojo.Employee;
import com.lele.service.IEmployeeService;
import com.lele.utils.RespPage;
import com.lele.utils.Result;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lele
 * @since 2022-11-26
 */
@RestController
@RequestMapping("/employee/basic")
public class EmployeeController {
    @Resource
    private IEmployeeService employeeService;
    @Resource
    private RabbitTemplate rabbitTemplate;


    @GetMapping("/")
    public RespPage getEmployee(@RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "employee", required = false) Employee employee,
                                LocalDate[] beginDateScope) {
        RespPage respPage = employeeService.getEmployee(pageNum, pageSize, employee, beginDateScope);
        return respPage;
    }


    @GetMapping("/export")
    public void exportEmployee(HttpServletResponse response) {
        List<Employee> employeeList = employeeService.exportEmployee();
        ExportParams params = new ExportParams("员工表", "员工表sheet1", ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, employeeList);
        ServletOutputStream out = null;
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            //防止中文乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("员工表.xlsx", "UTF-8"));
            out = response.getOutputStream();
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        //可以直接读取在resources下的资源文件的位置，
        ClassPathResource pathResource = new ClassPathResource("excel/河南省疾病预防控制中心CDC实验室信息系统需求变动说明书V1.0-20220722-学习培训考试(1).docx");
        String path = pathResource.getFile().getPath();
        File file = new File(path);
        //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //防设置文件名和文件名编码类型
        response.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
        //此时是ppt
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("河南省疾病预防控制中心CDC实验室信息系统需求变动说明书V1.0-20220722-学习培训考试(1).docx", "UTF-8"));
        try {
            FileInputStream inputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.copy(inputStream, outputStream);
            //刷新输出流
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/tosendmail")
    public Result toSendMail(@RequestParam("id") Integer id) {
        Employee employee=employeeService.sendMail(id);
        return Result.OK();
    }
}
